/**
 * http://code.google.com/p/gwt-spot
 * 
 * GWT-Spot -- Copyright 2008-2009
 *          -- by Dunkel, Ricardo
 *  
 */
package de.ricardodunkel.gwtspot.client.api.common;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.user.client.ui.RootPanel;

import de.ricardodunkel.gwtspot.client.api.core.constant.TAG;

/**
 * Basic utility class for <em>meta</em> tag handling.
 * 
 * <p>
 * 	Provides several functions for meta tag handling to 
 *  enable dynamic change i.e. of <em>keywords</em>.
 * </p>
 * 
 * @author Dunkel, Ricardo
 *
 * @version 0.1
 * 
 */
public class MetaUtility {
	
	
	// ------------ DECRIPTION ------------------------------------------------
	/**
	 * Retrieve content of <strong>description</strong>
	 * meta tag as {@link String}.
	 * 
	 * @return  The <em>META</em> tag {@link TAG#DESCRIPTION} content value.
	 */
	public static String getDescription() {
		return getDescriptionElement().getPropertyString( TAG.CONTENT );
	}
	
	/**
	 * Retrieve content of <strong>description</strong>
	 * meta tag as {@link Element}.
	 * 
	 * @return  The <em>META</em> tag {@link TAG#DESCRIPTION} element.
	 */
	public static Element getDescriptionElement() {
		return getMetaElement( TAG.DESCRIPTION );
	}

	/**
	 * Does replace a meta tag descriptions content.
	 * 
	 * @param description  A description to set.
	 */
	public static void changeDescription(String description) {
		changeMetaData( TAG.DESCRIPTION, description );
	}
	
	// ------------ KEYWORDS --------------------------------------------------
	/**
	 * Retrieve content of <strong>keyword</strong>
	 * meta tag as {@link String}.
	 * 
	 * @return  The <em>META</em> tag {@link TAG#KEYWORDS} content value.
	 */
	public static String getKeywords() {
		return getKeywordElement().getPropertyString( TAG.CONTENT );
	}
	
	/**
	 * Retrieve content of <strong>keyword</strong>
	 * meta tag as {@link Element}.
	 * 
	 * @return  The <em>META</em> tag {@link TAG#KEYWORDS} element.
	 */
	public static Element getKeywordElement() {
		return getMetaElement( TAG.KEYWORDS );
	}

	/**
	 * Does replace a meta tag keyword content.
	 * 
	 * @param keywords  A set of keywords to replace.
	 */
	public static void changeKeywords(String keywords) {
		changeMetaData(TAG.KEYWORDS, keywords);
	}
	
	// ------------ COMMON ----------------------------------------------------
	/**
	 * Retrieve an element from meta data.
	 * 
	 * @param attribute  A certain attribute of meta tags to look up for.
	 * 
	 * @return  An element matching to desired attribute.
	 */
	public static Element getMetaElement(String attribute) {
		NodeList<Element> notes = RootPanel.get().getElement().getOwnerDocument().getElementsByTagName(TAG.META);
		Element e = null;
       	for( int i = 0; i < notes.getLength(); i ++ ) {
       		if( notes.getItem(i).getAttribute(TAG.NAME).equals(attribute) ) {
       			e = notes.getItem(i);
       		}
       	}
		return e;
	}
	
	/**
	 * Does change content of given meta element.
	 * 
	 * @param element  An element which should be updated.
	 * @param content  A content string representing new content.
	 */
	public static void changeMetaData(String element, String content) {
		if( null == content ) {
			content = "";
		}
		if( null != getKeywordElement() ) {
			getMetaElement(element).setPropertyString(TAG.CONTENT, content);
		}
	}

}
